bpo-32259: Make a TypeError message when unpack non-iterable more specific.#4903
Conversation
|
|
||
| assert(v != NULL); | ||
|
|
||
| if (v->ob_type->tp_iter == NULL && !PySequence_Check(v)) { |
There was a problem hiding this comment.
Would it be possible to move this test to after the !PyErr_Occurred() test a few lines below? That would take the PySequence() call out of the non-error code path.
There was a problem hiding this comment.
Sorry, I don't understand you. The only !PyErr_Occurred() below is the one inside a loop at line 4154. Do you suggest to move this test after the loop? This doesn't make sense to me, since the loop is run if the argument is an iterable, and this test tests if it is not an iterable.
There was a problem hiding this comment.
Ah, you're right, sorry.
My point was that you've added a test every time an object is unpacked, which happens a lot. I guess the only way around that is to move this test to after the it == NULL test, and change the text of the error if it's a TypeErrror and if the conditions on this line hold. I'm not sure if it's worth that to avoid the PySequence_Check() call, though. Maybe a quick micro-benchmark would say it's nothing to worry about. I'm not in front of a "real" computer to test, though.
There was a problem hiding this comment.
This makes sense, thanks.
|
This looks okay to me. |
| v->ob_type->tp_iter == NULL && !PySequence_Check(v)) | ||
| { | ||
| PyErr_Format(PyExc_TypeError, | ||
| "cannot unpack %.200s object", |
There was a problem hiding this comment.
cannot unpack %.200s object (it is not iterable)? or cannot unpack %.200s object because it is not iterable?
https://bugs.python.org/issue32259